﻿using System;
using Pantry.Business;
using Pantry.Entity;

namespace Pantry.Web.Users
{
    public partial class AddSupportingMaterial : System.Web.UI.Page
    {
        private SupportingMaterialBus _supBus;
        private SupportingMaterial _sm;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                _supBus = new SupportingMaterialBus();
                DropDownListOrgName.DataSource = _supBus.SelectOrgName();
                DropDownListOrgName.DataTextField = "Name";
                DropDownListOrgName.DataValueField = "Id";
                DropDownListOrgName.DataBind();
                TextBoxAddedBy.Text = Session["user"].ToString();
            }
        }

        protected void CreateSupportClick(object sender, EventArgs e)
        {
            _supBus = new SupportingMaterialBus();
            string orgId = Request.QueryString["id"];

            _sm = new SupportingMaterial
                      {
                          Url = TextBoxUrl.Text,
                          Description = TextBoxDescription.Text,
                          AddedBy = TextBoxAddedBy.Text,
                          AddedDate =
                              string.IsNullOrEmpty(TextBoxAddedDate.Text)
                                  ? DateTime.Now
                                  : DateTime.Parse(TextBoxAddedDate.Text),
                          Type = DropDownListType.SelectedItem.Text,
                          UserName = Session["user"].ToString(),
                          OrgId = int.Parse(DropDownListOrgName.SelectedValue)
                      };
            int i = _supBus.InsertSupportingMaterial(_sm);
            Page.ClientScript.RegisterStartupScript(GetType(), "", i > 0
                                                        ? "showmessage('Insert supporting material successfully !','SUCCESS','','false');"
                                                        : "showmessage('Cannot insert supporting material !','ERROR','','false');",
                                                    true);
            Response.Redirect("OrganisationDetails.aspx?id=" + orgId + "&action=update&#table_wTabs-5");
        }

        protected void BackListSupportClick(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["id"];
            Response.Redirect("OrganisationDetails.aspx?id=" + orgId + "&action=update&#table_wTabs-5");
        }
    }
}
